
package me.zhengjie.modules.workOrder.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.workOrder.domain.WorkOrderDetail;
import me.zhengjie.modules.workOrder.service.WorkOrderDetailService;
import me.zhengjie.modules.workOrder.service.dto.WorkOrderDetailQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author 李奎
* @date 2023-06-29
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工单明细管理")
@RequestMapping("/api/workOrderDetail")
public class WorkOrderDetailController {

    private final WorkOrderDetailService workOrderDetailService;

    @Log("工单明细导出数据")
    @ApiOperation("工单明细导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('workOrderDetail:list')")
    public void exportWorkOrderDetail(HttpServletResponse response, WorkOrderDetailQueryCriteria criteria) throws IOException {
        workOrderDetailService.download(workOrderDetailService.queryAll(criteria), response);
    }

    @Log("工单明细导入数据")
    @ApiOperation("工单明细导入数据")
    @GetMapping(value = "/import")
    @PreAuthorize("@el.check('workOrderDetail:list')")
    public void importDetail(@RequestParam("file") MultipartFile file, String headId) throws Exception {
        workOrderDetailService.importDetail(file,headId);
    }

    @GetMapping
    @Log("查询工单明细")
    @ApiOperation("查询工单明细")
    @PreAuthorize("@el.check('workOrderDetail:list')")
    public ResponseEntity<Object> queryWorkOrderDetail(WorkOrderDetailQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(workOrderDetailService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工单明细")
    @ApiOperation("新增工单明细")
    @PreAuthorize("@el.check('workOrderDetail:add')")
    public ResponseEntity<Object> createWorkOrderDetail(@Validated @RequestBody WorkOrderDetail resources){
        return new ResponseEntity<>(workOrderDetailService.create(resources),HttpStatus.CREATED);
    }

    @GetMapping("/getDetailById")
    @Log("工单明细查询")
    @ApiOperation("工单明细查询")
    public ResponseEntity<Object> getDetailById(String id){
        return new ResponseEntity<>(workOrderDetailService.findById(id),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工单明细")
    @ApiOperation("修改工单明细")
    @PreAuthorize("@el.check('workOrderDetail:edit')")
    public ResponseEntity<Object> updateWorkOrderDetail(@Validated @RequestBody WorkOrderDetail resources){
        workOrderDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工单明细")
    @ApiOperation("删除工单明细")
    @PreAuthorize("@el.check('workOrderDetail:del')")
    public ResponseEntity<Object> deleteWorkOrderDetail(@RequestBody String[] ids) {
        workOrderDetailService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}